package com.leador.http.callback;

import com.leador.http.ResultParse;
import com.leador.manger.LBSTraceClient;
import com.leador.module.response.Result;
import com.leador.module.response.fence.AlarmResult;
import com.leador.module.response.fence.HisAlarmResult;
import com.leador.module.response.fence.FenceCreateResult;
import com.leador.module.response.fence.FenceResult;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by liuha on 2016/9/18.
 */
public class FenceCallBack {
    private static  FenceCreateResult fenceSucessCallback(String msg){
        FenceCreateResult result = new FenceCreateResult();
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(msg);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if(jsonObject == null){
            return null;
        }

        String status = null;
        try {
            status = jsonObject.getString("status");
            result.setStatus(status);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String message = null;
        try {
            message = jsonObject.getString("message");
            result.setMessage(message);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String fence_id = null;
        try {
            fence_id = jsonObject.getString("fence_id");
            result.setFence_id(fence_id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return result;
    }
    public static void fenceCallBack(int status,String message){
        if(LBSTraceClient.onGeoFenceListener == null){
            return;
        }
        try{
            Result result;
            switch (status){
                case 0:
                    result = ResultParse.resultParse(message);
                    LBSTraceClient.onGeoFenceListener.onRequestFailedCallback(result);
                    break;
                case 1:
                    FenceCreateResult fenceCreateResult = fenceSucessCallback(message);
                    LBSTraceClient.onGeoFenceListener.onCreateCircularFenceCallback(fenceCreateResult);
                    break;
                case 2:
                    FenceCreateResult fenceCreateResult1 = fenceSucessCallback(message);
                    LBSTraceClient.onGeoFenceListener.onCreateVertexesFenceCallback(fenceCreateResult1);
                    break;
                case 3:
                    result = ResultParse.resultParse(message);
                    LBSTraceClient.onGeoFenceListener.onUpdateFenceCallback(result);
                    break;
                case 4:
                    result = ResultParse.resultParse(message);
                    LBSTraceClient.onGeoFenceListener.onDeleteFenceCallback(result);
                    break;
                case 5:
                    FenceResult fenceResult = ResultParse.fenceListParse(message);
                    LBSTraceClient.onGeoFenceListener.onQueryFenceListCallback(fenceResult);
                    break;
                case 6:
                    AlarmResult alarmResult = ResultParse.alarmParse(message);
                    LBSTraceClient.onGeoFenceListener.onQueryMonitoredStatusCallback(alarmResult);
                    break;
                case 7:
                    HisAlarmResult alarmsResult = ResultParse.alarmssResult(message);
                    LBSTraceClient.onGeoFenceListener.onQueryHistoryAlarmCallback(alarmsResult);
                    break;
            }
        }catch (Exception e){

        }
    }
}
